#!/usr/bin/perl
#edit_protocol.cgi

require './protocolman-lib.pl';

&ReadParse();

#edit protocol's property
if ($in{'opertype'} =~ /p/)
{
	if ($in{'protocoltype'} =~ /W/)
	{
		&foreign_require("samba", "samba-lib.pl");
		%econfig = &foreign_config("samba");
		&foreign_call("samba", "get_share", "global");
		if (!($in{'source'} =~ /cache/))
		{
			$in{'serverdescin'} = &foreign_call("samba", "getval", "server string");
			$in{'securityrdn'} = &foreign_call("samba", "getval", "security");
			$in{'oldsecurity'} = $in{'securityrdn'};
			$in{'codesel'} = &foreign_call("samba", "getval", "client code page");
			$in{'wgname'} = &foreign_call("samba", "getval", "workgroup");
#			my $umap = &foreign_call("samba", "getval", "username map");
#			open(UMAP, $umap);
#			while(<UMAP>) {
#				s/\r|\n//g;
#				s/[#;].*$//g;
#				if (/^\s*(\S+)\s*=\s*(.*)$/)
#				{
#					local $uunix = $1;
#					local $rest = $2;
#					while($rest =~ /^\s*"([^"]*)"(.*)$/ ||
#		   		   	$rest =~ /^\s*(\S+)(.*)$/) {
#						push(@uunix, $uunix);
#						push(@uwin, $1);
#						$rest = $2;
#					}
#				}
#			}
#			close(UMAP);
#			$in{'mapcache'} = "";
#			for($i=0; $i<@uunix; $i++)
#			{
#				$in{'mapcache'} .="$uunix[$i]=$uwin[$i]";
#				$in{'mapcache'} .=":"  if ($i <(@uunix - 1));
#			}
		}

		&header($text{'cifs_protocol_property'}, "", "editcifs", 0, 1);
#		print "<hr>\n";
		print "<form action=edit_protocol.cgi method=post onsubmit='document.forms[0].saveproperty.disabled = true;'><table class=mode1>\n";
		print "<tr> <td><b>$text{'server_desc'}</b><input type=hidden name=source value='cache'>\n";
		print "<input type=hidden name=opertype value='p'><input type=hidden name=protocoltype value='W'><input type=hidden name=oldsecurity value=$in{'oldsecurity'}></td>\n";

		print "<td><input name=serverdescin class=text size=30 value='$in{'serverdescin'}'></td> </tr>\n";
		
		print "<tr><td><b>", ($in{'securityrdn'} =~ /^domain$/)?$text{'domainname'}:$text{'workgroupname'}, "</b></td><td><I>$in{'wgname'}</I></td></tr>";

		print "<tr> <td valign=top><b>$text{'security_mode'}</b></td>\n";
		print "<td><table class=mode1>\n";

		printf "<tr> <td><input type=radio name=securityrdn %s value='share' onclick=\'return cifs_share_clk(\"$in{'securityrdn'}\");\'>$text{'share_mode'}</td></tr>\n", ($in{'securityrdn'} =~ /^share$/)?"checked":"";
#		print "<td>$text{'sharemode_hint'}</td> </tr>\n";
		printf "<tr> <td><input type=radio name=securityrdn %s value='user' onclick=\'return cifs_user_clk(\"$in{'securityrdn'}\");\'>$text{'user_mode'}</td></tr>\n", ($in{'securityrdn'} =~ /^user$/)?"checked":"";
#		print "<td>$text{'usermode_hint'}</td> </tr>\n";

		if($in{'securityrdn'} eq "domain")
		{
			printf "<tr> <td><input type=radio name=securityrdn %s value='domain'>$text{'domain_mode'}</td></tr>\n", ($in{'securityrdn'} =~ /^domain$/)?"checked":"";
		}

#		print "<input name=securityrdn type=hidden value=\"$in{'securityrdn'}\">";

#		print "<td>$text{'domainmode_hint'}<a href=\"/servermin/\"><b>$text{'addtodomain'}</b></a></td> </tr>\n";
		print "</table></td> </tr>\n";
		print "<tr> <td valign=top><b>$text{'code_page'}</b></td>\n";
		print "<td><select name=codesel>\n";
		printf "<option %s value='850'>$text{'english'}\n", ($in{'codesel'} eq '850')?"selected":"";
		printf "<option %s value='936'>$text{'sim_chinese'}\n", ($in{'codesel'} eq '936')?"selected":"";
		print "</select></td> </tr>\n";
#		print "<tr> <td valign=top>$text{'username_map'}</td>\n";

#		printf "<td><table><tr> <td><input type=radio name=optionrdn %s value='0'>$text{'none'}\n", ($in{'mapcache'})?"":"checked";
#		printf "<input type=radio name=optionrdn %s value='1'>$text{'list_below'}</td> </tr>\n", ($in{'mapcache'})?"checked":"";
#		print "<tr> <td><table border>\n";
#		print "<tr $tb> <td>*</td><td>$text{'unix_users'}</td>\n";
#		print "<td>$text{'windows_users'}<input type=hidden name=mapcache value='$in{'mapcache'}'></td> </tr>\n";
#		my @maps = split(":", $in{'mapcache'});
#		$n = @maps;
#		foreach $d (@maps)
#		{
#			$d =~ /^(.*)=(.*)$/;
#			print "<tr $cb> <td><input type=radio name=delselectrdn value='y'></td>\n";
#			printf "<td><input name=umap_unix size=8 value=\"%s\" onchange='cachemaplist($n)'></td>\n", $1;
#			printf "<td><input name=umap_win size=30 value=\"%s\" onchange='cachemaplist($n)'></td> </tr>\n", $2;
#		}

#		print "</table></td> </tr>\n";
#		print "<tr> <td><input name=adduuserin size=8>=<input name=addwuserin>\n";
#		print "<input type=button name=addbtn value=$text{'add'} onclick='addtomap($n)'>\n";
#		print "<input type=button name=removebtn value=$text{'remove'} onclick='removefrommap($n)'></td> </tr>\n";
#		print "</table></td> </tr>\n";
		print "</table><input type=submit name=saveproperty class=button1 value=$text{'submit'} onclick='setupactionstatus()'></form>\n";
	}
	elsif ($in{'protocoltype'} =~ /U/)
	{
		&header($text{'nfs_protocol_property'}, "", undef, 0, 1);
#		print "<hr>\n";
		print $text{'noproperty'};
	}
	elsif ($in{'protocoltype'} =~ /F/)
	{
		my (@selectshare, %sharetab1, %sharetab2, $k, $sharepath, @selectoption, %gbconfig, @line);
		
		&header($text{'ftp_protocol_property'}, "", "editftp", 0, 1);
		&foreign_require("pureftpd", "pureftpd-lib.pl");
		&foreign_call("pureftpd", "get_global_config", \%gbconfig);

		print "<form action=edit_protocol.cgi method=post onsubmit='document.forms[0].saveproperty.disabled = true;'>\n";

		$in{"maxclient"} = $gbconfig{'MaxClientsNumber'} if ($in{"maxclient"} eq "");
		$in{"client_per_ip"} = $gbconfig{'MaxClientsPerIP'} if ($in{"client_per_ip"} eq "");
		$sharepath = &foreign_call("pureftpd", "get_anonymous_home") if ($sharepath eq "");
		
		if ($in{"anonyclient"} eq "")
		{
			@line = split(":", $gbconfig{"PerUserLimits"});
			$in{"anonyclient"} = $line[1];
		}
		if ($in{"anony_bw"} eq "")
		{
			$in{"anony_bw"} = $gbconfig{'AnonymousBandwidth'};
			if($in{"anony_bw"} >= 1024)
			{
				$in{"anony_bw"} /= 1024;				
				$in{"anony_bw"} = sprintf ".2f", $in{"anony_bw"} if($in{"anony_bw"} =~ /\./);
				$in{"bw_unit"} = 1;
			}
		}
		if($in{"sharename"} eq "")
		{
			&foreign_require("shareman", "shareman-lib.pl");
			&foreign_call("shareman", "read_sharetab", \%sharetab2, 2);   #sharepath is key
			if($sharetab2{$sharepath}->{"sharename"})
			{
				$in{"sharename"} = $sharetab2{$sharepath}->{"sharename"};
				$in{"setanony"} = "yes" if($in{"setanony"} eq "");
			}
			else
			{
				$in{"sharename"} = "";
				$sharepath = "";
			}
		}

		$in{"setanony"} = "no" if($in{"setanony"} eq "");

		print "<table class=mode1 border=0>\n";
		print "<tr><td><b>$text{'max_client'}</b></td><td colspan=2><input name=maxclient class=text value=\"$gbconfig{'MaxClientsNumber'}\"></td>\n";
		if(!$gbconfig{'MaxClientsPerIP'}){
			$gbconfig{'MaxClientsPerIP'} = "0";
		}
		print "<tr><td><b>$text{'max_client_per_ip'}</b></td><td colspan=2><input name=client_per_ip class=text value=\"$gbconfig{'MaxClientsPerIP'}\">";
		if(!$gbconfig{'MaxClientsPerIP'}){
			print $text{'zero_mean_nolimit'};
		}
		print "<td></tr>\n";
		if(!$gbconfig{'Bind'}){
			$ftpbind = "21";
		}
		else{
			$ftpbind = $gbconfig{'Bind'};
		}
		print "<tr><td><b>$text{'portno'}</b></td><td colspan=2><input name=portno class=text value=\"$ftpbind\"></td>";
		print "<tr><td><b>$text{'set_anony'}<b></td><td colspan=2><input type=radio name=setanony value=\"yes\" ", $in{'setanony'} eq 'yes' ? 'checked':'', " onclick=\' RefreshPage();\'>$text{'yes'}<input type=radio name=setanony value=\"no\" ", $in{'setanony'} eq 'no' ? 'checked':'' ," onclick=\' RefreshPage();\' >$text{'no'}</td></tr>\n";
		if($in{"setanony"} eq "yes")
		{
			print "<tr><td><b>$text{'anony_root'}</b></td><td colspan=2>\n";
			&foreign_require("shareman", "shareman-lib.pl");
			&foreign_call("shareman", "read_sharetab", \%sharetab1, 1);   #sharename is key
			@selectshare = ();
			push @selectshare, "<select name=sharename>\n";
			push @selectshare, "<option value=\'\'>$text{'nosetshare'}\n";
			foreach $k (sort keys %sharetab1)
			{
				if($k eq $in{"sharename"})
				{
					push @selectshare, "<option value=\'$k\' selected>$k\n";  #keys is sharename, value is sharepath. print sharenam in select
				}
				else
				{
					push @selectshare, "<option value=\'$k\'>$k\n";
				}
			}
			if($in{"sharename"} eq "")
			{
				$selectshare[1] = "<option value=\'\' selected>$text{'nosetshare'}\n";
			}
			if(@selectshare < 3)
			{
				$selectshare[1] = "<option value=\'\'>$text{'noshare'}\n";
			}

			push @selectshare, "</select>\n";
			
			print @selectshare;
			
			print "</td></tr>\n";

			print "<tr><td><b>$text{'anony_client'}</b></td><td colspan=2><input class=text name=anonyclient value=\"$in{'anonyclient'}\"></td></tr>\n";
			print "<tr><td><b>$text{'anony_bw'}</b></td><td>";
			print "<table border=0>";
			print "<tr><td><input class=text size=6 name=anony_bw value=\"$in{'anony_bw'}\"></td><td>";
			@selectoption = ("<option value='0'>KB", "<option value='1'>MB");
			print "<select name=bw_unit size=1>";
			if($in{'bw_unit'} eq "0")
			{
				$selectoption[0] = "<option selected value='0'>KB";
			}
			if($in{'bw_unit'} eq "1")
			{
				$selectoption[1] = "<option selected value='1'>MB";
			}
			print @selectoption;
			print "</select>";
			print "</td></tr>";
			print "</table>";
			print "</td></tr>\n";
		}
		print "</table>\n";

		print "<p><input type=submit name=saveproperty value=$text{'submit'} class=button1 onclick='document.forms[0].action = \"save_ftp.cgi\";'>\n";
		print "<p><input type=hidden name=protocoltype value='F'>";
		print "<p><input type=hidden name=opertype value=\'$in{'opertype'}\'>";
		print "</form>\n";
	}
	elsif ($in{'protocoltype'} =~ /H/)
	{
#		&foreign_require("apache", "apache-lib.pl");
#		&foreign_require("apache", "auth-lib.pl");
#		%econfig = &foreign_config("apache");

#		if (!($in{'source'} =~ /cache/))
#		{
#			@grps = &foreign_call("apache", "list_authgroups", $config{'authgroup_file'});
#			$in{'groupcache'} = "";
#			for ($i = 0; $i < @grps; $i ++)
#			{
#				$in{'groupcache'} .=($grps[$i]->{'group'}.":");
#				@mems = @{$grps[$i]->{'members'}};
#				$in{'groupcache'} .= join(" ", @mems) if (@mems);
#				$in{'groupcache'} .= ";" if ($i < (@grps -1));
#			}

#		}

		&header($text{'http_protocol_property'}, "", undef, 0, 1);
#		print "<hr>\n";
#		print "<form action=edit_protocol.cgi method=post><table>\n";
#		print "<tr> <td>$text{'bind_addr'}</td>\n";
#		printf "<td><input type=radio %s name=httpaddrrdn value='0'>$text{'all_address'}\n", (@bind)?"":"checked";
#		printf "<input type=radio %s name=httpaddrrdn value='1'>\n", (@bind)?"checked":"";
#		print "<input name=httpaddrin value=$addrin></td> </tr>\n";
#		print "<tr> <td valign=top>$text{'http_groups'}<input type=hidden name=source value='cache'></td>\n";
#		print "<td><input type=hidden name=opertype value='p'><input type=hidden name=protocoltype value='H'><table><tr> <td><table border>\n";
#		print "<tr $tb> <td>*</td><td>$text{'group_name'}<input type=hidden name=groupcache value='$in{'groupcache'}'></td><td>$text{'members'}</td> </tr>\n";
#		my @maps = split(";", $in{'groupcache'});
#		$n = @maps;
#		foreach $d (@maps)
#		{
#			$d =~ /^(.*):(.*)$/;
#			print "<tr $cb> <td><input type=radio name=delgrouprdn value='y'></td>\n";
#			printf "<td><input name=groupnamein size=8 value=\"%s\" onchange='cachegrouplist($n)'></td>\n", $1;
#			printf "<td><input name=membersin size=30 value=\"%s\" onchange='cachegrouplist($n)'></td> </tr>\n", $2;
#		}
#		print "</table></td> </tr>\n";
#		printf "<tr> <td><input name=addgroupin size=8>:<input name=addmembersin size=30> %s\n", &user_chooser_button("addmembersin", 1);
#		print "<input type=button name=addgroupbtn value=$text{'add'} onclick='addtogroupmap($n)'>\n";
#		print "<input type=button name=removegroupbtn value=$text{'remove'} onclick='removefromgroupmap($n)'></td> </tr>\n";
#		print "</table></td> </tr>\n";
#		print "</table>\n";
#		print "<input type=submit value=$text{'submit'} onclick='setupactionhttp()'></form>\n";
		print $text{'noproperty'};
	}
	elsif ($in{'protocoltype'} =~ /A/)
	{
		my (%servconf);
		
		&header($text{'afp_protocol_property'}, "", "editapple", 0, 1);
		#print "<table class=mode1>\n";

		&foreign_require("netatalk", "netapple-lib.pl");
		&foreign_call("netatalk", "ez_get_afpserver", \%servconf);

		$in{'afp_timeout'} = $servconf{'timeout'};
		$in{'afp_timeout'} =~ s/-timeout\s+//;
		$in{'afp_timeout'} =~ s/\s+$//;
		$in{'afp_timeout'} = ceil($in{'afp_timeout'} / 2);

		$in{'afp_session'} = &foreign_call("netatalk", "ez_get_maxsessions");
		
		if($servconf{'uamlist'} =~ /uams_guest.so/)
		{
			$in{'afp_allowguest'} = "yes";
		}
		
		print "<form action=edit_protocol.cgi method=post onsubmit='document.forms[0].saveproperty.disabled = true;'>\n";

		printf "<p><input type=checkbox name=afp_allowguest value='yes' %s >$text{'afp_allowguest'}", ($in{'afp_allowguest'} eq 'yes') ? "checked":"";

		print "<p><table class=mode1>\n";
		print "<tr><td>$text{'afp_timeout'}</td><td><input class=text name=afp_timeout size=8 value=\'$in{'afp_timeout'}\'></td></tr>\n";

		print "<tr><td>$text{'afp_session'}</td><td><input class=text name=afp_session size=8 value=\'$in{'afp_session'}\'></td></tr>\n";

		print "</table>\n";

		print "<p><input type=submit name=saveproperty value=$text{'submit'} class=button1 onclick='return afp_submit_clk();'>\n";
		print "<p><input type=hidden name=protocoltype value='A'>";
		print "<p><input type=hidden name=opertype value=\'$in{'opertype'}\'>";
	}


print <<EOF;
<script>
function afp_submit_clk()
{
       if(document.forms[0].afp_timeout.value == 0)
       {
               alert("$text{'err_msg_afp_timeout0'}");
               return false;
       }

       if(document.forms[0].afp_session.value == 0)
       {
               alert("$text{'err_msg_afp_session0'}");
               return false;
       }

       document.forms[0].action = "save_atalk.cgi";
}

function cifs_share_clk(security)
{
        if(security == "domain")
        {
                 alert("$text{'msg_share_clk'}");
        }
}

function cifs_user_clk(security)
{
        if(security == "domain")
        {
                 alert("$text{'msg_user_clk'}");
        }
}

function RefreshPage()
{
	document.forms[0].action = 'edit_protocol.cgi';
	document.forms[0].submit();
}

function addtomap(n)
{
	document.forms[0].mapcache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].umap_unix.length; i++)
		{
			document.forms[0].mapcache.value +=(document.forms[0].umap_unix[i].value +"="+
							document.forms[0].umap_win[i].value+ ":");

		}
	}
	else if (n == 1)
	{
		document.forms[0].mapcache.value +=(document.forms[0].umap_unix.value +"="+
							document.forms[0].umap_win.value+ ":");
	}
	document.forms[0].mapcache.value +=(document.forms[0].adduuserin.value +"="+
							document.forms[0].addwuserin.value);
	document.forms[0].adduuserin.value = "";
	document.forms[0].addwuserin.value = "";
	RefreshPage();
}

function removefrommap(n)
{
	var found = -1;
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].umap_unix.length; i++)
		{
			if (document.forms[0].delselectrdn[i].checked)
			{
				found = i;
				break;
			}
		}
	}
	else if (n == 1)
	{
		if (document.forms[0].delselectrdn.checked)
		{
			found = 0;
		}
	}
	if (found >= 0)
	{
		document.forms[0].mapcache.value = "";
		if (n > 1)
		{
			for (i = 0; i < document.forms[0].umap_unix.length; i++)
			{
				if (i != found)
				{
					if (document.forms[0].mapcache.value != "")
						document.forms[0].mapcache.value +=":";
					document.forms[0].mapcache.value +=(document.forms[0].umap_unix[i].value +"="+
							document.forms[0].umap_win[i].value);
				}
			}
		}
		RefreshPage();
	}
	else
	{
		alert("$text{'noselectmap'}");
	}
}

function setupactionstatus()
{
	document.forms[0].action = 'save_cifs.cgi';
}

function setupactionhttp()
{
	document.forms[0].action = 'save_http.cgi';
}

function cachemaplist(n)
{
	document.forms[0].mapcache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].umap_unix.length; i++)
		{
			document.forms[0].mapcache.value +=(document.forms[0].umap_unix[i].value +"="+
							document.forms[0].umap_win[i].value);
			if (i < (document.forms[0].umap_unix.length - 1))
				document.forms[0].mapcache.value += ":";
		}
	}
	else if (n == 1)
	{
		document.forms[0].mapcache.value +=(document.forms[0].umap_unix.value +"="+
							document.forms[0].umap_win.value);
	}
}

function cachegrouplist(n)
{
	document.forms[0].groupcache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].groupnamein.length; i++)
		{
			document.forms[0].groupcache.value +=(document.forms[0].groupnamein[i].value +":"+
							document.forms[0].membersin[i].value);
			if (i < (document.forms[0].groupnamein.length - 1))
				document.forms[0].groupcache.value += ";";
		}
	}
	else if (n == 1)
	{
		document.forms[0].groupcache.value +=(document.forms[0].groupname.value +":"+
							document.forms[0].membersin.value);
	}
}

function addtogroupmap(n)
{
	document.forms[0].groupcache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].groupnamein.length; i++)
		{
			document.forms[0].groupcache.value +=(document.forms[0].groupnamein[i].value +":"+
							document.forms[0].membersin[i].value+ ";");

		}
	}
	else if (n == 1)
	{
		document.forms[0].groupcache.value +=(document.forms[0].groupnamein.value +":"+
							document.forms[0].membersin.value+ ";");
	}
	document.forms[0].groupcache.value +=(document.forms[0].addgroupin.value +":"+
							document.forms[0].addmembersin.value);
	document.forms[0].addgroupin.value = "";
	document.forms[0].addmembersin.value = "";
	RefreshPage();

}

function removefromgroupmap(n)
{
	var found = -1;
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].groupnamein.length; i++)
		{
			if (document.forms[0].delgrouprdn[i].checked)
			{
				found = i;
				break;
			}
		}
	}
	else if (n == 1)
	{
		if (document.forms[0].delgrouprdn.checked)
		{
			found = 0;
		}
	}
	if (found >= 0)
	{
		document.forms[0].groupcache.value = "";
		if (n > 1)
		{
			for (i = 0; i < document.forms[0].groupnamein.length; i++)
			{
				if (i != found)
				{
					if (document.forms[0].groupcache.value != "")
						document.forms[0].groupcache.value +=";";
					document.forms[0].groupcache.value +=(document.forms[0].groupnamein[i].value +":"+
							document.forms[0].membersin[i].value);
				}
			}
		}
		RefreshPage();
	}
	else
	{
		alert("$text{'noselectgroup'}");
	}
}

</script>
EOF

#	print "<hr>\n";
	&footer("index.cgi",  $text{'list_protocols_return'});
}
else
{
	#start, stop ,restart service
	if ($in{'protocoltype'} =~ /W/)
	{
		$commandstr = $config{'cifs_service'};
		$loginfo = " cifs service";
	}
	elsif ($in{'protocoltype'} =~ /U/)
	{
		$commandstr = $config{'nfs_service'};
		$loginfo = " nfs service";
	}
	elsif ($in{'protocoltype'} =~ /F/)
	{
		$commandstr = $config{'ftp_service'};
		$loginfo = " ftp service";
	}
	elsif ($in{'protocoltype'} =~ /H/)
	{
		$commandstr = $config{'http_service'};
		$loginfo = " http service";
	}
	elsif ($in{'protocoltype'} =~ /A/)
	{
		$commandstr = $config{'afp_service'};
		$loginfo = " afp service";
		if($in{'opertype'} =~ /s|r/)
		{
			if(-f $config{'bondinglock'})
			{
				&error_setup($text{"service_start_error"});
				&error($text{'err_msg_atalk_bonding'});
			}
		}
	}

	if ($in{'opertype'} =~ /s/)
	{
		$commandstr .= " start";
		$loginfo = "Start".$loginfo;
	}
	elsif ($in{'opertype'} =~ /t/)
	{
		$commandstr .= " stop";
		$loginfo = "Stop".$loginfo;
	}
	elsif ($in{'opertype'} =~ /r/)
	{
		$commandstr .= " restart";
		$loginfo = "Restart".$loginfo;
	}

	`$commandstr`;
	&nas_syslog("protocolman", "naslog_info", 32000, $loginfo);

	&redirect("index.cgi");
}
